<template>
    <div class="chart-wrap">
       <div class="title">
          <span v-text="name"></span>
          <div class="tools">
            <el-select v-model="query.topnumber" placeholder="请选择">
              <el-option
                v-for="item in dateTypeList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
            <el-date-picker
              v-model="query.startDate"
              type="date"
              placeholder="选择日期"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd">
            </el-date-picker>
            <el-date-picker
              v-model="query.endDate"
              type="date"
              placeholder="选择日期"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd">
            </el-date-picker>
          </div>
       </div>
       <div class="chart">
           <div ref="chart" class="content" ></div>
       </div>
    </div>
</template>

<script>
    import mixin from '../mixs/mixin.js';
    import 'echarts-wordcloud';
    import echarts from "echarts";
    export default {
        name: "chart_1" ,
        mixins: [mixin],
        props: {
          name: {
            type: String,
            default: '表名称'
          },
          category:{
            type: String,
            default: 'news' //分类 news,paper,patent
          }
        },
        data() {
          let startDate = new Date(`${new Date().getFullYear()}/1/1`).Format('yyyy-MM-dd');
          let endDate = new Date().Format('yyyy-MM-dd');
          return {
            apiUrl:'newtechanalyse/hotWordsAtlas',
            dateTypeList:[
              {label:'10',value:'10'},
              {label:'20',value:'20'}
            ],
            query:{
              // startDate:startDate, //开始时间
              // endDate:endDate, //结束时间
             // topnumber:'10', //Month,Day 查询时间类型
              category:this.category, //分类 News,Paper,Patent
            },
            hotwords:[]
          }
        },
        mounted(){
          this._initChart();
        },
        methods: {
          _initChart() {
             this.$nextTick(()=>{
                 if(!this._vmEchart){
                   this._vmEchart = echarts.init(this.$refs.chart);
                   this._vmEchart.showLoading();
                 }
             })
          },
          asyncDate(){
            //  异步加载数据  通用方法
            if(!this._vmEchart) return;
            this._vmEchart.clear();
            this._vmEchart.showLoading();
            this.api({
              url: this.apiUrl,
              method: "post",
              data:this.$qs.stringify({...this.query,topicId:this.navCurrentId})
            }).then(data => {
               this._formatDate(data)
            });
          },
          getDataURL(){
            // 获取图片base64
            return new Promise((resolve,reject)=>{
              if(!this._vmEchart){reject(); return; }
              resolve(this._vmEchart.getDataURL({
                backgroundColor: this.exportEchartBg
              }))
            });
          },
          _formatDate(data){
            // 根据不同的数据 配合不同的方法
            let hotWords = [];
            data.words.forEach(item => {
              hotWords.push({
                name: item.label,
                value: 100,
                // Style of single text
                textStyle: {
                  normal: {},
                  emphasis: {}
                }
              })
            });
            this.hotwords = hotWords;
            this._setOption();
          },
          _setOption(){
             let $this = this;
             let option = {
               series: [{
                 type: 'wordCloud',

                 // The shape of the "cloud" to draw. Can be any polar equation represented as a
                 // callback function, or a keyword present. Available presents are circle (default),
                 // cardioid (apple or heart shape curve, the most known polar equation), diamond (
                 // alias of square), triangle-forward, triangle, (alias of triangle-upright, pentagon, and star.

                 shape: 'circle',

                 // A silhouette image which the white area will be excluded from drawing texts.
                 // The shape option will continue to apply as the shape of the cloud to grow.

                 //  maskImage: maskImage,

                 // Folllowing left/top/width/height/right/bottom are used for positioning the word cloud
                 // Default to be put in the center and has 75% x 80% size.

                 left: 'center',
                 top: 'center',
                 width: '70%',
                 height: '80%',
                 right: null,
                 bottom: null,

                 // Text size range which the value in data will be mapped to.
                 // Default to have minimum 12px and maximum 60px size.

                 sizeRange: [20, 40],

                 // Text rotation range and step in degree. Text will be rotated randomly in range [-90, 90] by rotationStep 45

                 rotationRange: [0, 0],
                 rotationStep: 45,

                 // size of the grid in pixels for marking the availability of the canvas
                 // the larger the grid size, the bigger the gap between words.

                 gridSize: 8,

                 // set to true to allow word being draw partly outside of the canvas.
                 // Allow word bigger than the size of the canvas to be drawn
                 drawOutOfBound: true,

                 // Global text style
                 textStyle: {
                   normal: {
                     fontFamily: 'sans-serif',
                     fontWeight: 'bold',
                     // Color can be a callback function or a color string
                     color: function () {
                       return '#00a3fa';
                     }
                   },
                   emphasis: {
                     shadowBlur: 10,
                     shadowColor: '#333'
                   }
                 },

                 // Data is an array. Each array item must have name and value property.
                 data: this.hotwords
               }]
             };

              this._vmEchart.hideLoading();
              this._vmEchart.setOption(option)
          }
        },


    }
</script>

<style lang="scss" scoped>
    .chart{
      position: relative;
      .content{
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
      }
    }

</style>
